﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FinanSoft.Utilities;
using FinanSoft.BusinessLayer;
using FinanSoft.Entities;

namespace FinanSoft
{
    public partial class frmCuentas : Form
    {
        List<int> _accountIds;

        public frmCuentas()
        {
            InitializeComponent();
            fillGridCatalogo();
        }

        private void addToGrid(Account parent, int level)
        {
            addRow(parent, level);
            if (parent.IsTitle)
                foreach (Account child in parent.Children.Values)
                    addToGrid(child, level+1);
        }

        private void addRow(Account account, int level)
        {
            _accountIds.Add(account.AccountID);
            gridCatalogo.Rows.Add(new object[] 
                    { FormUtilities.AddTabs(account.AccountCode, level), account.Name, account.ForeignName, account.AccountCurrency.Abbreviation,
                        account.AccountCurrencyAmount.ToString("F")+" "+account.AccountCurrency.Symbol});
        }

        private void fillGridCatalogo()
        {
            gridCatalogo.Rows.Clear();
            AccountingCatalog catalog = CatalogManager.GetInstance().GetAccountingCatalog();
            if (!CatalogManager.GetInstance().IsError)
            {
                _accountIds = new List<int>();
                foreach (Account mainAccount in catalog.PrincipalAccounts)
                    addToGrid(mainAccount, 0);
            }
            else
            {
                MessageBox.Show(CatalogManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
        }

        private void btnSalir_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnCrearCuenta_Click(object sender, EventArgs e)
        {
            // Cargamos y mostramos el formulario Crear Cuenta
            frmCrearCuenta frmCrearCuentaV = new frmCrearCuenta(Constants.INVALID_ID);
            frmCrearCuentaV.ShowDialog(this);     // Mostramos el formulario de forma modal.
            fillGridCatalogo();
        }

        private void btnEliminarCuenta_Click(object sender, EventArgs e)
        {
            if (gridCatalogo.SelectedRows.Count == 0)
                MessageBox.Show(MessageConstants.NO_SELECTED_ACCOUNT, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                int deletedId = _accountIds[gridCatalogo.SelectedRows[0].Index];
                CatalogManager.GetInstance().DeleteAccount(deletedId);
                if (CatalogManager.GetInstance().IsError)
                    MessageBox.Show(CatalogManager.GetInstance().ErrorMessage, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    MessageBox.Show(MessageConstants.ACCOUNT_DELETED, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    fillGridCatalogo();
                }
            }
        }

        private void btnEditarCuenta_Click(object sender, EventArgs e)
        {
            // Cargamos y mostramos el formulario Crear Cuenta
            if (gridCatalogo.SelectedRows.Count == 0)
                MessageBox.Show(MessageConstants.NO_SELECTED_ACCOUNT, MessageConstants.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                int editedId = _accountIds[gridCatalogo.SelectedRows[0].Index];
                frmCrearCuenta frmCrearCuentaV = new frmCrearCuenta(editedId);
                frmCrearCuentaV.ShowDialog(this);     // Mostramos el formulario de forma modal.
                fillGridCatalogo();
            }

        }
    }
}
